import { Configures } from "../const/Configures";
import { Login_Status } from "../const/EnumName";
import { EventName } from "../const/EventName";
import Events from "./Events";


const { ccclass, property } = cc._decorator;

@ccclass
export class DataCtr extends cc.Component {
    public static _dict={}
    public static _name_dict={}

    // 后期需要优化的点。
    // 此处是数据全部加载。
    // 理想的处理方式是。只有用到的时候才进行加载 内存 > 储存 > 网络获取

    public static async init() {
        //用于读取远段的测试数据
        if (Configures.test_data != '') {
            await new Promise((resolve, reject) => {
                cc.assetManager.loadRemote(Configures.test_data, cc.JsonAsset, (err, asset) => {
                    if (err) {
                        reject(err)
                    } else {
                        this.sortJson(asset)
                        resolve(null)
                    }
                })
            })
        } else {
            // Load.getRes('data/game_data', cc.JsonAsset, this.sortJson.bind(this), null)
            let asset = await new Promise((resolve, reject) => {
                cc.resources.load(Configures.data_path, cc.JsonAsset, (error: Error, resource: cc.JsonAsset) => {
                    if (error) {
                        cc.error(`[Res.load] error: ${error}`);
                        resolve(null);
                    } else {
                        resolve(resource);
                    }
                });
            });
            this.sortJson(asset)
        }
    }


    public static sortJson(asset) {
        if (asset) {
            for (let key in asset.json) {
                this._dict[key] = asset.json[key]
            }
            // Tabel Name
            // 具体作用就是 表名容错
            // 容错名 ：正确名
            this._name_dict = {
                
            }
            //这里本来是登录流程，暂时用不到
            // Events.emit(EventName.LOGIN_EVENT,Login_Status.LOAD_RES)
        } else {
            // Events.emit(EventName.LOGIN_EVENT,Login_Status.LOAD_RES)
        }
        // return
    }
    public static getTable(tName) {
        if (this._dict[tName]) {
            return this._dict[tName]
        }
        let newTname = null
        for (let key in this._name_dict) {
            if (key == tName) {
                newTname = this._name_dict[key]
                break
            }
        }
        if (newTname)
            return this._dict[newTname]
        return null
    }
    public static getData(tName, id) {
        let table = this.getTable(tName)
        if (table) {
            for (let i = 0; i < table.length; i++) {
                if (table[i].id == id) {
                    return table[i]
                }
            }
            console.error("没有数据" + tName + ",数据id:" + id)
            return null
        } else {
            console.error("没有对应表" + tName)
            return null
        }
    }
    public static getDataByIndex(tName, index) {
        let table = this.getTable(tName)
        if (table) {
            if (table[index]) {
                return table[index]
            }
            console.error("没有数据" + tName + ",数据index:" + index)
            return null
        } else {
            console.error("没有对应表" + tName)
            return null
        }
    }
    public static getDataByArgs(tName, arg) {
        let table = this.getTable(tName)
        if (table) {
            for (let i = 0; i < table.length; i++) {
                let add = true
                for (let key in arg) {
                    if (table[i][key] != arg[key]) {
                        add = false
                    }
                }
                if (add) {
                    return table[i]
                }
            }
            return null
        } else {
            console.error("没有对应表" + tName)
            return null
        }
    }
    public static getDataListByArgs(tName, arg) {
        let table = this.getTable(tName)
        if (table) {
            let list = []
            for (let i = 0; i < table.length; i++) {
                let add = true
                for (let key in arg) {
                    if (table[i][key] != arg[key]) {
                        add = false
                    }
                }
                if (add) {
                    list.push(table[i])
                }
            }
            if (list.length) {
                return list
            } else {
                return null
            }
        } else {
            console.error("没有对应表" + tName)
            return null
        }
    }
}
